{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "wYtuKeK0dImp"
   },
   "outputs": [],
   "source": [
    "import csv\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "EmMyh9_mkDHF"
   },
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "IcLOZlnnc_N7"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "4kxw-_rmcnVu"
   },
   "outputs": [],
   "source": [
    "def get_data(filename):\n",
    "  # You will need to write code that will read the file passed\n",
    "  # into this function. The first line contains the column headers\n",
    "  # so you should ignore it\n",
    "  # Each successive line contians 785 comma separated values between 0 and 255\n",
    "  # The first value is the label\n",
    "  # The rest are the pixel values for that picture\n",
    "  # The function will return 2 np.array types. One with all the labels\n",
    "  # One with all the images\n",
    "  #\n",
    "  # Tips: \n",
    "  # If you read a full line (as 'row') then row[0] has the label\n",
    "  # and row[1:785] has the 784 pixel values\n",
    "  # Take a look at np.array_split to turn the 784 pixels into 28x28\n",
    "  # You are reading in strings, but need the values to be floats\n",
    "  # Check out np.array().astype for a conversion\n",
    "    with open(filename) as training_file:\n",
    "      # Your code starts here\n",
    "      # Your code ends here\n",
    "    return images, labels\n",
    "\n",
    "\n",
    "training_images, training_labels = get_data('sign_mnist_train.csv')\n",
    "testing_images, testing_labels = get_data('sign_mnist_test.csv')\n",
    "\n",
    "# Keep these\n",
    "print(training_images.shape)\n",
    "print(training_labels.shape)\n",
    "print(testing_images.shape)\n",
    "print(testing_labels.shape)\n",
    "\n",
    "# Their output should be:\n",
    "# (27455, 28, 28)\n",
    "# (27455,)\n",
    "# (7172, 28, 28)\n",
    "# (7172,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "awoqRpyZdQkD"
   },
   "outputs": [],
   "source": [
    "# In this section you will have to add another dimension to the data\n",
    "# So, for example, if your array is (10000, 28, 28)\n",
    "# You will need to make it (10000, 28, 28, 1)\n",
    "# Hint: np.expand_dims\n",
    "\n",
    "training_images = # Your Code Here\n",
    "testing_images = # Your Code Here\n",
    "\n",
    "# Create an ImageDataGenerator and do Image Augmentation\n",
    "train_datagen = ImageDataGenerator(\n",
    "    # Your Code Here\n",
    "    )\n",
    "\n",
    "validation_datagen = ImageDataGenerator(\n",
    "    # Your Code Here)\n",
    "    \n",
    "# Keep These\n",
    "print(training_images.shape)\n",
    "print(testing_images.shape)\n",
    "    \n",
    "# Their output should be:\n",
    "# (27455, 28, 28, 1)\n",
    "# (7172, 28, 28, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Rmb7S32cgRqS"
   },
   "outputs": [],
   "source": [
    "# Define the model\n",
    "# Use no more than 2 Conv2D and 2 MaxPooling2D\n",
    "model = tf.keras.models.Sequential([\n",
    "    # Your Code Here\n",
    "    )\n",
    "\n",
    "# Compile Model. \n",
    "model.compile(# Your Code Here)\n",
    "\n",
    "# Train the Model\n",
    "history = model.fit_generator(# Your Code Here)\n",
    "\n",
    "model.evaluate(testing_images, testing_labels)\n",
    "    \n",
    "# The output from model.evaluate should be close to:\n",
    "[6.92426086682151, 0.56609035]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "_Q3Zpr46dsij"
   },
   "outputs": [],
   "source": [
    "# Plot the chart for accuracy and loss on both training and validation\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "acc = # Your Code Here\n",
    "val_acc = # Your Code Here\n",
    "loss = # Your Code Here\n",
    "val_loss = # Your Code Here\n",
    "\n",
    "epochs = range(len(acc))\n",
    "\n",
    "plt.plot(epochs, acc, 'r', label='Training accuracy')\n",
    "plt.plot(epochs, val_acc, 'b', label='Validation accuracy')\n",
    "plt.title('Training and validation accuracy')\n",
    "plt.legend()\n",
    "plt.figure()\n",
    "\n",
    "plt.plot(epochs, loss, 'r', label='Training Loss')\n",
    "plt.plot(epochs, val_loss, 'b', label='Validation Loss')\n",
    "plt.title('Training and validation loss')\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "Exercise 8 - Question.ipynb",
   "provenance": [
    {
     "file_id": "1KMi9wAhMhxvuc-bfptkCUlnVRGrh5ySj",
     "timestamp": 1550682934823
    }
   ],
   "version": "0.3.2"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
